﻿
using IotContract.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using XT.MNet.Helpers;
using XT.MNet.Tcp;
using XT.MNet.Tcp.Options;

namespace EcsSimulator.Services
{
    public class SocketClientServices
    {

        private TcpClient _clientServer;


        private bool _status = false;

        public event EventHandler<ConnectStatusEventArgs> OnConnectStatusChanged;

        public event EventHandler<string> Receive;

        public async Task<bool> Start(string ip,int Port)
        {
            try
            {
                _clientServer = new TcpClient(new TcpClientOptions()
                {
                    SpecialChar = '#',
                    Address = ip,
                    Port = (ushort)Port
                });
       

                _clientServer.On(data =>
                {
                    var da = Encoding.UTF8.GetString(data.ToArray());
                    Receive.Invoke(null,da);
                });

                _clientServer.OnConnect += _clientServer_OnConnect;
                _clientServer.OnDisconnect += _clientServer_OnDisconnect;

                _clientServer.Connect();

     
            }
            catch (Exception ex)
            {
               
            }

            return true;
        }

        private void _clientServer_OnDisconnect()
        {
            throw new NotImplementedException();
        }

        private void _clientServer_OnConnect()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <returns></returns>
        public async Task ClientSend(Memory<Byte> memory)
        {
            _clientServer.Send(memory);
        }



    }
}
